package com.xilihui.xlh.business.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.AccountInfoActivity;
import com.xilihui.xlh.business.activitys.CommissionActivity;
import com.xilihui.xlh.business.activitys.CreditScoreActivity;
import com.xilihui.xlh.business.activitys.IntegralActivity;
import com.xilihui.xlh.business.activitys.LoginActivity;
import com.xilihui.xlh.business.activitys.RegisterActivity;
import com.xilihui.xlh.business.activitys.SettingActivity;
import com.xilihui.xlh.business.activitys.WithdrawActivity;
import com.xilihui.xlh.business.entities.LoginEntity;
import com.xilihui.xlh.business.requests.MineRequest;
import com.xilihui.xlh.business.widget.CircleImageView;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatFragment;
import com.xilihui.xlh.core.glide.ImageHelper;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/16.
 */

public class MineFragment extends BaseCompatFragment{

    @BindView(R.id.sdv_head)
    SimpleDraweeView sdv_head;
    @BindView(R.id.tv_nick)
    TextView tv_nick;
    @BindView(R.id.iv_class)
    ImageView iv_class;
    
    String nick = "";
    String url = "";
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        YEventBuses.register(this);
        setInfo();
        
    }
    
    public void setInfo(){
        nick = (String) SPUtil.get(getContext(),SPUtil.NICK,"");
        tv_nick.setText(nick);
        url = (String)SPUtil.get(getContext(),SPUtil.HEAD_IMAGE,"");
        url = UrlConst.baseUrl() +url;
        sdv_head.setImageURI(url);
        String sign_status = (String)SPUtil.get(getContext(),SPUtil.SIGN,"");
        if(sign_status.equals("普通会员")){
            iv_class.setImageResource(R.mipmap.ic_unsign);
        }else {
            iv_class.setImageResource(R.mipmap.ic_signing);
        }
    }
    
    @OnClick(R.id.cl_info)
    public void info(){
        startActivity(new Intent(getContext(), AccountInfoActivity.class));
    }
    
    @OnClick(R.id.rl_yongji)
    public void yongji(){
        startActivity(new Intent(getContext(), CommissionActivity.class));
    }
    
    @OnClick(R.id.rl_integral)
    public void integral(){
        startActivity(new Intent(getContext(), IntegralActivity.class));
    }
    
    @OnClick(R.id.rl_credit_score)
    public void creditScore(){
        startActivity(new Intent(getContext(), CreditScoreActivity.class));
    }
    
    @OnClick(R.id.rl_setting)
    public void setting(){
        startActivity(new Intent(getContext(), SettingActivity.class));
    }

    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        if(event.is("Mine")){
            getUserInfo();
            LogUtil.i("mylog","mine");
        }else if(event.is("login_")){
            getUserInfo();
        }
    }
    
    public void getUserInfo(){
        
        MineRequest.mine(getContext())
                .compose(DoTransform.<LoginEntity>applyScheduler(getActivity(),true))
                .compose(this.<LoginEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<LoginEntity>(getActivity()) {
                    @Override
                    protected void onSuccess(LoginEntity entity) {
                        SPUtil.put(getContext(),SPUtil.NICK,entity.getNickname());
                        SPUtil.put(getContext(),SPUtil.HEAD_IMAGE,entity.getHead_pic());
                        SPUtil.put(getContext(),SPUtil.SIGN,entity.getSign_status());
                        
                        sdv_head.setImageURI(UrlConst.baseUrl()+entity.getHead_pic());
                        tv_nick.setText(entity.getNickname());
                        if(entity.getSign_status().equals("普通会员")){
                            iv_class.setImageResource(R.mipmap.ic_unsign);
                        }else {
                            iv_class.setImageResource(R.mipmap.ic_signing);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        YEventBuses.unregister(this);
    }
}
